#include "AVUtils.h"
#include "core/utils/AudioStreamUtils.h"
#include <ohaudio/native_audiostreambuilder.h>


void AVUtils::AVFormatDeleter::operator()(OH_AVFormat *fmt) {
    if (fmt) {
        OH_AVFormat_Destroy(fmt);
    }
}

std::unique_ptr<OH_AVFormat, AVUtils::AVFormatDeleter> AVUtils::makeAVFormat(const std::function<OH_AVFormat *()> &ctor) {
    OH_AVFormat *fmt = ctor();
    return std::unique_ptr<OH_AVFormat, AVFormatDeleter>(fmt, AVFormatDeleter());
}

void AVUtils::AudioStreamBuilderDeleter::operator()(OH_AudioStreamBuilder *bld) {
    if (bld) {
        AS_CALL_OR_ELSE(OH_AudioStreamBuilder_Destroy(bld), return);
    }
}

std::unique_ptr<OH_AudioStreamBuilder, AVUtils::AudioStreamBuilderDeleter> AVUtils::makeAudioStreamBuilder(OH_AudioStream_Type type) {
    OH_AudioStreamBuilder *bld;
    AS_CALL_OR_ELSE(OH_AudioStreamBuilder_Create(&bld, type), return nullptr);
    return std::unique_ptr<OH_AudioStreamBuilder, AudioStreamBuilderDeleter>(bld, AudioStreamBuilderDeleter());
}
